package com.mxx.common.core;

import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.*;

public final class Options<T> {

    private T value;
    public static final Options<?> EMPTY = new Options<>();

    private Options() {
    }

    private Options(T value) {
        this.value = Objects.requireNonNull(value);
    }

    public static <T> Options<T> of(T value) {
        return new Options<T>(value);
    }

    public static <T> Options<T> ofNullable(T value) {
        return value == null ? empty() : new Options<>(value);
    }

    @SuppressWarnings("unchecked")
    public static <T> Options<T> empty() {
        return (Options<T>) EMPTY;
    }


    public static <E> E current(E e) {
        return e;
    }


    public <U> Options<U> map(Function<? super T, ? extends U> mapper) {
        Objects.requireNonNull(mapper);
        if (isPresent()) {
            return Options.ofNullable(mapper.apply(value));
        } else {
            return empty();
        }
    }

    public <U> Options<U> flatMap(Function<? super T, Options<U>> mapper) {
        Objects.requireNonNull(mapper);
        if (!isPresent())
            return empty();
        else {
            return Objects.requireNonNull(mapper.apply(value));
        }
    }

    public Options<T> exec(Consumer<? super T> consumer) {
        Objects.requireNonNull(consumer);
        if (isPresent()) {
            consumer.accept(value);
            return this;
        } else {
            return empty();
        }
    }

    public <E> Options<T> exec(Predicate<T> predicate, E e, Consumer<? super E> consumer, Consumer<? super E> elseConsumer) {
        Objects.requireNonNull(predicate);
        Objects.requireNonNull(e);
        Objects.requireNonNull(consumer);
        Objects.requireNonNull(elseConsumer);
        if (isPresent()) {
            if (predicate.test(value)) {
                consumer.accept(e);
            } else {
                elseConsumer.accept(e);
            }
            return this;
        } else {
            return empty();
        }
    }

    public Options<T> exec(Predicate<T> predicate, Consumer<? super T> consumer, Consumer<? super T> elseConsumer) {
        if (isPresent()) {
            return exec(predicate, this.value, consumer, elseConsumer);
        } else {
            return empty();
        }
    }

    public Options<T> elseExec(Runnable runnable) {
        Objects.requireNonNull(runnable);
        if (!isPresent()) {
            runnable.run();
        }
        return this;
    }

    public Options<T> filter(Predicate<? super T> predicate) {
        Objects.requireNonNull(predicate);
        if (!isPresent()) {
            return this;
        } else {
            return predicate.test(value) ? this : empty();
        }
    }

    public <V1> Options<T> filter(SFunction<? super T, V1> sFunction, Predicate<V1> predicate) {
        Objects.requireNonNull(sFunction);
        Objects.requireNonNull(predicate);
        if (!isPresent()) {
            return this;
        } else {
            V1 v1 = sFunction.apply(value);
            return predicate.test(v1) ? this : empty();
        }
    }

    public <V1, V2> Options<T> filter(SFunction<? super T, V1> sFunction1, SFunction<? super T, V2> sFunction2, BiPredicate<V1, V2> predicate) {
        Objects.requireNonNull(sFunction1);
        Objects.requireNonNull(sFunction2);
        Objects.requireNonNull(predicate);
        if (!isPresent()) {
            return this;
        } else {
            V1 v1 = sFunction1.apply(value);
            V2 v2 = sFunction2.apply(value);
            return predicate.test(v1, v2) ? this : empty();
        }
    }

    public <V1, V2, V3> Options<T> filter(SFunction<? super T, V1> sFunction1, SFunction<? super T, V2> sFunction2, SFunction<? super T, V3> sFunction3, BTPredicate<V1, V2, V3> predicate) {
        Objects.requireNonNull(sFunction1);
        Objects.requireNonNull(sFunction2);
        Objects.requireNonNull(sFunction3);
        Objects.requireNonNull(predicate);
        if (!isPresent()) {
            return this;
        } else {
            V1 v1 = sFunction1.apply(value);
            V2 v2 = sFunction2.apply(value);
            V3 v3 = sFunction3.apply(value);
            return predicate.test(v1, v2, v3) ? this : empty();
        }
    }


    public T get() {
        if (value == null) {
            throw new NoSuchElementException("No value present");
        }
        return value;
    }

    public <V> V newVal(Supplier<V> supplier) {
        return Objects.requireNonNull(supplier).get();
    }

    public T orElse(T other) {
        return value != null ? value : other;
    }

    public T orElseGet(Supplier<? extends T> other) {
        return value != null ? value : other.get();
    }

    public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
        if (value != null) {
            return value;
        } else {
            throw exceptionSupplier.get();
        }
    }

    public void ifPresent(Consumer<? super T> consumer) {
        if (value != null)
            consumer.accept(value);
    }

    public <X extends Throwable> Options<T> elseThrow(Supplier<? extends X> exceptionSupplier) throws X {
        if (value != null) {
            return this;
        } else {
            throw exceptionSupplier.get();
        }
    }

    public boolean isPresent() {
        return value != null;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof Options)) {
            return false;
        }

        Options<?> other = (Options<?>) obj;
        return Objects.equals(value, other.value);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(value);
    }


    @Override
    public String toString() {
        return value != null
                ? String.format("Options[%s]", value)
                : "Options.empty";
    }
}
